import pygame

import src.const

pygame.init()

img_bg = pygame.image.load('img/bg.jpg')
img_icon = pygame.image.load('img/icon.png')
img_plane = pygame.image.load('img/plane.png')

screen_width = src.const.screen_width
screen_height = src.const.screen_height

plane_width = img_plane.get_width()
plane_height = img_plane.get_height()

pos_x = (screen_width - plane_width) / 2
pos_y = screen_height - plane_width - 15

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_icon(img_icon)

running = True
is_keydown = False
down_key = 0
while running:
    screen.blit(img_bg, (0, 0))
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            is_keydown = True
            down_key = event.key
        elif event.type == pygame.KEYUP:
            is_keydown = False
            down_key = 0
    if is_keydown and down_key == pygame.K_LEFT:
        if pos_x > 0:
            pos_x -= 1
    elif is_keydown and down_key == pygame.K_RIGHT:
        if pos_x + plane_width < screen_width:
            pos_x += 1

    screen.blit(img_plane, (pos_x, pos_y))
    pygame.display.update()
